#coding:utf-8
import time,os,logging
from str2arry import str2arry
from serialCom import serialCom
from checkSum import hex_checksum

FILE = os.getcwd()          #返回当前目录的路径
#logging基础配置，文件名为 os.path.join里面的两个值想加个，输出等级为≥DEBUG级别
logging.basicConfig(filename = os.path.join(FILE,'log.txt'),level = logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')

def setStatus():
    print '----------------------------------------------------------'
    print 'case4 设置工作状态' + '\n' + '                                    '
    logging.debug('*****case4 设置工作状态*****')
    # 设置工作状态，格式
    frameHead = 'FE FE FE FE FE 68 AB 57 58'
    # meterID
    frameBody= 'F2 77 88 99 55 04 00 06 A0 23 01 00 00'
    stateCode={'03':'正常态','02':'检测态','01':'校正态'}
    # 校验和
    # 16

    # 读取表号
    meterIDread = (
        ''.join(('FE FE FE FE FE 68 AB 57 58 AA AA AA AA AA F2 77 88 99 55 03 00 03 81 0A 01 85 16').split()))
    ID = serialCom(meterIDread, 35)[28:38]
    ID = str2arry(ID)                                #返回值为数组
    logging.debug('获取表号 %s 用于设置参数',''.join(ID))
    # 读取表号
    for i in sorted(stateCode.keys(),reverse=True):
        frameData = frameHead.split() + ID + frameBody.split() + i.split()
        frameCheckSum=hex_checksum(frameData[5:])
        frame=''.join(frameData+frameCheckSum)+'16'
        serialCom(frame)
        logging.debug('设置 %s'%stateCode[i])
        time.sleep(5)

    frameData = frameHead.split() + ID + frameBody.split() + ['03']
    frameCheckSum = hex_checksum(frameData[5:])
    frame = ''.join(frameData + frameCheckSum) + '16'
    serialCom(frame)
    time.sleep(5)
    logging.debug('返回 %s' %stateCode['03'])



